package com.bishe.app.modules.rolemenu.controller;

import com.bishe.app.core.api.CommonResult;
import com.bishe.app.core.api.CommonPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.app.core.enums.IsDel;
import com.bishe.app.core.utils.Lock;
import com.bishe.app.modules.rolemenu.model.BatchCreateRoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bishe.app.modules.rolemenu.service.RoleMenuService;
import com.bishe.app.modules.rolemenu.entity.RoleMenu;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 角色-菜单表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-13
 */
@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {

    @Autowired
    public RoleMenuService roleMenuService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/roleMenu",key = "lock", lockSecond = 60)
    public CommonResult create(@RequestBody RoleMenu roleMenu) {
        boolean success = roleMenuService.save(roleMenu);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/batchCreate", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody BatchCreateRoleMenu roleMenu) {
        roleMenuService.lambdaUpdate()
                .set(RoleMenu::getIsDel, IsDel.E.getCode())
                .eq(RoleMenu::getRoleId, roleMenu.getRoleId())
                .update();

        // batch insert
        List<RoleMenu> rmList = new ArrayList<>();
        for (Long mid : roleMenu.getMenuIdList()) {
            RoleMenu rm = new RoleMenu();
            rm.setRoleId(roleMenu.getRoleId());
            rm.setMenuId(mid);
            rmList.add(rm);
        }

        boolean success = roleMenuService.saveBatch(rmList);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody RoleMenu roleMenu) {
        roleMenu.setId(id);
        boolean success = roleMenuService.updateById(roleMenu);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = roleMenuService.removeById(id);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean success = roleMenuService.removeByIds(ids);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }


    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<RoleMenu>> listAll() {
        List<RoleMenu> roleMenuList = roleMenuService.list();
        return CommonResult.success(roleMenuList);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<RoleMenu>> page(@RequestBody CommonPage commonPage) {
        Page<RoleMenu> page = new Page(commonPage.getPageNum(), commonPage.getPageSize());
        return CommonResult.success(CommonPage.restPage(roleMenuService.page(page)));
    }
}



